package com.sumainfor.wx.tenant.controller.alipay;


import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.google.gson.JsonObject;
import com.sumainfor.common.alipayutils.ConfigUtils;
import com.sumainfor.common.alipayutils.SSLClient;
import com.sumainfor.common.alipayutils.SignUtils;
import com.sumainfor.common.alipayutils.ZhiFuBaoUtil.ZhiFuMessage;
import com.sumainfor.common.util.HttpUtilBee;
import com.sumainfor.common.util.JsonResult;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Meng
 * @ClassName ZfbController
 * @date 2019/12/9 18:40
 * @description: TODO
 */
@Controller
@RequestMapping("/ZfbController")
public class ZfbController {

//    //获取用户编号
//    @ResponseBody
//    @PostMapping("/authorizationOne")
//    public JsonResult  authorizationOne(@RequestBody Map<String,Object> params){
//        JsonResult result = new JsonResult();
//
//        //实例化客户端
//        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", ZhiFuMessage.ALIPAY_APP_ID, ZhiFuMessage.APPLICATION_PRIVATE_KEY, "json", "GBK", ZhiFuMessage.ALIPAY_PUBLIC_KEY, "RSA2");
//
//        //  APP_ID = 2018012102010762
//
//        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
//
//        AlipaySystemOauthTokenRequest request = null;
//        try {
//            request = new AlipaySystemOauthTokenRequest();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        request.setGrantType("authorization_code");
//        request.setCode("766b64bdd8ab4d339e73bc2c9d7cPE47");
//        //request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
//        AlipaySystemOauthTokenResponse response = null;
//        try {
//            response = alipayClient.execute(request);
//            System.out.println(response);
//        } catch (AlipayApiException e) {
//            e.printStackTrace();
//        }
//        if(response.isSuccess()){
//            System.out.println("调用成功");
//            return result.put(response);
//        } else {
//            System.out.println("调用失败");
//            return result.putFailed("调用失败");
//        }
//
//
//    }


    @ResponseBody
    @PostMapping("/ceshierweima")
    public JsonResult ceshierweima(@RequestBody Map<String,Object> params){

        JsonResult result = new JsonResult();
        DefaultHttpClient httpClient = null;
        try {
            httpClient = new SSLClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
        HttpPost postMethod = new HttpPost("https://www.zfzlpay.com/payment-gate-web/gateway/api/backTransReq");
        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
        nvps.add(new BasicNameValuePair("requestNo", new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())));//请求流水号
        nvps.add(new BasicNameValuePair("version", "V1.0"));//版本号 3.0为异步通知返回卡类型
        nvps.add(new BasicNameValuePair("productId", "0109"));//产品类型
        //        nvps.add(new BasicNameValuePair("acctNo", "6217001180033516058"));//卡号 0129必传
        //        nvps.add(new BasicNameValuePair("customerName", "李浩"));//持卡人 0129必传
       // nvps.add(new BasicNameValuePair("openid", params.get("openid").toString()));//0105必须填写openid。0108时为sub_appid(传进来的openid)
        //       nvps.add(new BasicNameValuePair("userId", "2088802646833765"));//0115  0145必须填写，支付宝的userid  伟华的支付宝  返回的tradeNo截取掉前两位后发送前端进行支付
        nvps.add(new BasicNameValuePair("transId", "10"));//交易类型
        nvps.add(new BasicNameValuePair("merNo", "885440059321286"));//机构商户号
        nvps.add(new BasicNameValuePair("terMerNo","8862900100000011021"));//终端商户号
        nvps.add(new BasicNameValuePair("orderDate", "20191217"));//订单日期
        nvps.add(new BasicNameValuePair("subMerNo", "100000001604001"));//商户识别id  8862900100000011005002支付宝   8862900100000011005001微信
        nvps.add(new BasicNameValuePair("orderNo", "20191217001"));//商户订单号
        nvps.add(new BasicNameValuePair("notifyUrl", "www.baidu.com"));//异步通知地址
        nvps.add(new BasicNameValuePair("transAmt", "1"));//交易金额，单位 分
        nvps.add(new BasicNameValuePair("commodityName","测试支付宝扫码" ));//商品名称
        nvps.add(new BasicNameValuePair("commodityDesc", "测试"));//商品描述
                nvps.add(new BasicNameValuePair("storeId", "tangchao0001"));//e0109/0110必须填写，门店编号,需要可区分开门店
               nvps.add(new BasicNameValuePair("terminalId", "tangchao0001"));//0109/0110必须填写，商户终端编号,需要可区分开门店
        //        nvps.add(new BasicNameValuePair("limitPay", "no_credit"));//微信支付宝可使用此参数指定不能使用信用卡支付
        nvps.add(new BasicNameValuePair("subChnlMerNo","100000001604"));//子商户号100000001399支付宝   100000001397微信
        nvps.add(new BasicNameValuePair("subMerName", "上海唐巢公共租赁住房运营有限公司"));//子商户号名称 必须为实际店名，不得用个人名称代替，否则会存在风控限制
        try {
            nvps.add(new BasicNameValuePair("signature", SignUtils.signData(nvps)));//签名
        } catch (Exception e) {
            e.printStackTrace();
        }
        String Attestation = "";
        try {
            postMethod.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
            HttpResponse resp = httpClient.execute(postMethod);
            String str = EntityUtils.toString(resp.getEntity(), "UTF-8");
            int statusCode = resp.getStatusLine().getStatusCode();
            if (200 == statusCode) {
                boolean signFlag = SignUtils.verferSignData(str);
                if (!signFlag) {
                    System.out.println("验签失败");
                    Attestation = "验签失败";

                }else {
                    System.out.println("验签成功");
                    System.out.println(str);
                    Attestation = str;
                }
            }else {

                Attestation = "返回失败";
            }

            System.out.println("返回错误码:" + statusCode);
        } catch (Exception e) {
            e.printStackTrace();
        }

         params.put("jieguo",Attestation);
        return result.put(params);





    }




}





